<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Cadastro extends Controller_GradeHorariaFrontEnd{
    public function before(){
//      die("aqui2");
        
    }
    public function action_index(){
//          die("aqui3");
        parent::before();
        if(Model_User::logged_in(Kohana::$config->load("grademagica")->webuser_role_name)) {
//            die("aqui2");    
            $this->view->body->content = Smarty_View::factory("1_register/my_data.tpl");		
            $this->view->body->content->user = Model_User::get_user();
//            die("logado");
        }else{
//            die("não logado");
            $this->view->body->content = Smarty_View::factory("1_register/register.tpl");		
            $this->view->body->content->signin = Smarty_View::factory("1_register/signin.tpl");		
            $this->view->body->content->login = Smarty_View::factory("1_register/login.tpl");		
            if(isset($_GET["redirect"])){
                $this->view->body->content->signin->redirect = $_GET["redirect"];
                $this->view->body->content->login->redirect = $_GET["redirect"];
            }
            if(Arr::get(Kohana::modules(), "facebook")) {
//                    $this->view->body->content->facebook = Smarty_View::factory("1_register/facebookLoginButton.tpl");
                    $this->view->body->content->facebook = Smarty_View::factory("facebookLoginButton.tpl");
            }
//		return $view;
        }
        $this->response->body($this->view);
    }
//    public function __construct() {}
//	
    public function action_login($redirect = '/',$template='auth/login.tpl') {
        try {
            if (Model_User::login($_POST['email'], $_POST['password'])) {
                    if (isset($_POST["redirect"]) && !empty($_POST["redirect"])) {
                            $redirect = $_POST["redirect"];
                    } else {
                            $redirect = "/";
                    }
                    Request::current()->redirect($redirect);
            } else {
                    $errors = array("login" => __("Invalid login"));
                    $this->response->body(Json_View::factory(array("content" => $errors)));
            }
        } catch (Exception $e) {
                $errors = array("_external" => array("Login" => $e->getMessage()));
                $this->response->body(Json_View::factory(array("content" => $errors)));
        }
    }
    public function action_logout() {
            Model_User::logout();
            Request::current()->redirect('/');
    }
    public function action_signin() {
		$user = Model_User::factory();
		try {
			if(Model_User::confirm_password($_POST['password'], $_POST['password_confirm'])) {
				if(Model_User::unique_email($_POST['email'])) {
					$user->set_name($_POST['name']);
					$user->set_email($_POST['email']);
					$user->set_password(Model_User::hash($_POST['password']));
					$user->save();
					$user->add_role('login');
					Request::current()->redirect("/");
				}
			}
		} catch (Validation_Exception $e) {
			$this->response->body(Json_View::factory(array("content" => $e->errors())));
		} catch (Exception $e) {
			$errors = array("email" => $e->getMessage());
			$this->response->body(Json_View::factory(array("content" => $errors)));
		}
	}
}